﻿using System;
using System.Data;
using System.Data.DataSetExtensions;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Linq;

namespace CProd
{
    public partial class frmViradas : Form
    {
        #region Declarações

        private readonly SqlDataAdapter _daBobines = new SqlDataAdapter("Select * from Producao", frmEntrada.conn);
        private SqlCommandBuilder _builder;
        private readonly DataTable _dtBobines = new DataTable("dtBobines");
        private readonly DataView _dvBobines = new DataView();
        private readonly BindingSource _srcBobines = new BindingSource();
        private bool _newRecord;
        private char _turno;

        #endregion

        public frmViradas()
        {
            InitializeComponent();
        }

        private void frmViradas_Load(object sender, EventArgs e)
        {

        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region Navegador

        private void btFirst_Click(object sender, EventArgs e)
        {
            _srcBobines.MoveFirst();
        }

        private void btLast_Click(object sender, EventArgs e)
        {
            _srcBobines.MoveLast();
        }

        private void btPrev_Click(object sender, EventArgs e)
        {
            _srcBobines.MovePrevious();
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            _srcBobines.MoveNext();
        }
        #endregion

        private void frmViradas_Resize(object sender, EventArgs e)
        {
            ShowIcon = WindowState == FormWindowState.Maximized;
        }

        #region Validação Teclas

        private void txtKilos_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && e.KeyChar != ',' && e.KeyChar != '.' && !Char.IsControl(e.KeyChar)) e.Handled = true;
        }

        private void txtKNC_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && e.KeyChar != ',' && e.KeyChar != '.' && !Char.IsControl(e.KeyChar)) e.Handled = true;
        }

        private void txtUnidades_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar)) e.Handled = true;
        }

        private void txtUNC_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) && !Char.IsControl(e.KeyChar)) e.Handled = true;
        }

        private void txtObs_TextChanged(object sender, EventArgs e)
        {
            txtObs.Text = txtObs.Text.Replace("\"", "");
        }

        private void txtDateTime_TextChanged(object sender, EventArgs e)
        {
            if (txtHora.Text.Trim().Length == 5) calculaTurno();            
        }

        #endregion

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tem a certeza?", "Apagar Registo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    _srcBobines.RemoveCurrent();
                    _daBobines.Update(_dtBobines);
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }

        private void btnInserir_Click(object sender, EventArgs e)
        {

        }

        private void btSave_Click(object sender, EventArgs e)
        {

        }

        private void calculaTurno()
        {
            DateTime time = DateTime.Parse(txtHora.Text);

            const string turno1 = "DDCCCBBBBAAACCCCDDDAAAABBBDD";
            const string turno2 = "AAADDDDDDDBBBBBBBCCCCCCCAAAA";
            const string turno3 = "CBBBAAAACCCDDDDAAABBBBDDDCCC";

            double difDias = Math.Truncate(DateTime.Parse(txtDate.Text).Subtract(Convert.ToDateTime("24-12-2006")).TotalDays);
            int y = Convert.ToInt32(difDias - Math.Truncate(Math.Truncate(difDias / 28) * 28));

            if (y == 0) y = 27;

            if (time >= DateTime.Parse("00:00") && time <= DateTime.Parse("08:00"))
                _turno = turno1[y];
            if (time >= DateTime.Parse("08:01") && time <= DateTime.Parse("16:00"))
                _turno = turno2[y];
            if (time >= DateTime.Parse("16:01") && time <= DateTime.Parse("23:59"))
                _turno = turno3[y];

            txtTurno.Text = _turno.ToString();
        }

        //Cancela Inserção
        private void frmViradas_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Escape || !pnlInsert.Visible) return;
            cancela();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {

        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            cancela();
        }

        private void botoesonoff(string nomebotao)
        {

        }

        private void cancela()
        {

        }

        private void frmViradas_Validated(object sender, EventArgs e)
        {
            
        }

        private void txtHora_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }
        
        private void txtHora_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (txtHora.TextLength < 5) txtHora.Focus() ;
        }

        private void txtUnidades_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtVirada_TextChanged(object sender, EventArgs e)
        {
            MessageBox.Show(txtVirada.Text.Length.ToString());            
        }

        private void txtVirada_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MessageBox.Show("EW");
        } 
    }
}